package com.qike.duola.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.common.collect.ImmutableMap;
import com.qike.duola.domain.DuolaDept;
import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.DuolaDeptService;
import com.qike.duola.service.IsysResourceService;
import com.qike.duola.service.IsysUserService;
import com.qike.duola.shiro.LoginUser;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mobile.device.Device;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@Api(description = "部门控制器")
@CrossOrigin
public class DeptController {

    private static Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private DuolaDeptService deptService;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加部门
     *
     * @return
     */
    @PostMapping("/deptAdd")
    @ApiOperation("添加部门")
    @ResponseBody
    public BaseResult deptAdd(@RequestBody DeptAddDto deptAddDto) {
        try {
            deptService.deptAdd(deptAddDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 编辑部门
     *
     * @return
     */
    @PostMapping("/deptEdit")
    @ApiOperation("编辑部门")
    @ResponseBody
    public BaseResult deptEdit(@RequestBody DeptEditDto deptEditDto) {
        try {
            deptService.deptEdit(deptEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 删除部门
     *
     * @return
     */
    @PostMapping("/deptDel")
    @ApiOperation("删除部门")
    @ResponseBody
    public BaseResult deptDel(@RequestBody DeptDelDto deptDelDto) {
        try {
            deptService.deptDel(deptDelDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 部门列表
     *
     * @return
     */
    @GetMapping("/deptList")
    @ApiOperation("部门列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = DeptListVo.class)
    })
    public BaseResult deptList(DeptListDto deptListDto) {
        try {
            DeptListVo vo = deptService.deptList(deptListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 部门下拉列表
     *
     * @return
     */
    @ApiOperation("部门下拉列表")
    @GetMapping("/deptPullList")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = DeptPullListVo.class)
    })
    public BaseResult deptPullList(String deptName) {
        try {
            List<DeptPullListVo> vo = deptService.deptPullList(deptName);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
